import {Component} from '@angular/core';
import {App, IonicPage, NavController} from 'ionic-angular';
import {FormBuilder, Validators} from "@angular/forms";
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";
import {ManagerProvider} from "@providers/interface/manager";

/**
 * Generated class for the ManagerLoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-manager-login',
  templateUrl: 'manager-login.html',
})
export class ManagerLoginPage {

  hide: boolean = false;//获取焦点时隐藏下面的图片

  constructor(private app: App,
              private navCtrl: NavController,
              private formBuilder: FormBuilder,
              private storage: StorageProvider,
              private dialog: DialogProvider,
              private manager: ManagerProvider,) {

  }

  loginForm = this.formBuilder.group({
    'userId': ['', [Validators.required, Validators.minLength(4)]],// 第一个参数是默认值
    'password': ['', [Validators.required, Validators.minLength(4)]],
    'isManager': []
  });

  urlPage(page: string) {
    this.navCtrl.push(page);
  }

  login(user, _event) {
    this.loginForManager(user);
  }

  //管理员登录
  loginForManager(user: any) {
    this.dialog.showLoading();
    this.manager.loginForManager(user).then((message) => {
      this.dialog.hideLoading();
      if (message) {
        let userInfo = message.USER;
        userInfo["isLoggedIn"] = true;
        this.storage.write('userInfo', userInfo);
        this.app.getRootNav().setRoot('ManagerTabsPage');
      }else{
        this.dialog.showAlert('登录失败,账号或密码错误！');
      }
    }).catch(() => {
      this.dialog.hideLoading();
      this.dialog.showAlert('登录失败！');
    })

  }

  push(page) {
    this.navCtrl.push(page)
  }

}
